package com.lenovo.util;

import java.util.Arrays;
import java.util.Hashtable;
import java.util.Random;

public class ShortUrl {
	public static Hashtable<String, Integer> tables = new Hashtable();
	public static Integer sumCount = 0;
	public static Integer pwdCount = 0;
	public static Integer sameCount = 0;
	public static Integer sameCount4 = 0;

	public static String[] getUrl(String url) {
		// 可以自定义生成MD5加密字符传前的混合KEY
		String key = "liuzhibo";
		// 要使用生成URL的字符
		String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h",
				"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
				"u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
				"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
				"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
				"U", "V", "W", "X", "Y", "Z"

		};
		// 对传入网址进行MD5加密
		String hex = Md5Util.MD5Encode(url);
		hex = Md5Util.MD5Encode(hex);
		String[] resUrl = new String[4];

		for (int i = 0; i < 4; i++) {
			// 把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
			String tmp = hex.substring(i * 8, (i + 1) * 8);
			// System.out.println("md5字符串" + i + ":" + tmp);
			long hexint = 0x3FFFFFFF & Long.parseLong(tmp, 16);
			// System.out.println("md5与运算:" + i + ":" + hexint);
			String outChars = "";
			for (int j = 0; j < 6; j++) {
				// 把得到的值与0x0000003D进行位与运算，取得字符数组chars索引
				int index = (int) (0x0000003D & hexint);
				// 把取得的字符相加
				outChars += chars[index];
				// 每次循环按位右移5位
				hexint = hexint >> 5;
			}
			// 把字符串存入对应索引的输出数组
			resUrl[i] = outChars;
		}

		return resUrl;
	}

	// 转化字符串为十六进制编码
	public static String toHexString(String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;// 0x表示十六进制
	}

	// 转换十六进制编码为字符串
	public static String toStringHex(String s) {
		if ("0x".equals(s.substring(0, 2))) {
			s = s.substring(2);
		}
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
						i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			s = new String(baKeyword, "utf-8");// UTF-16le:Not
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

	/**
	 * 产生随机字符串
	 * */
	private static Random randGen = null;
	private static char[] numbersAndLetters = null;
	private static Object initLock = new Object();

	public static final String randomString(int length) {

		if (length < 1) {
			return null;
		}
		if (randGen == null) {
			synchronized (initLock) {
				if (randGen == null) {
					randGen = new Random();
					numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz"
							+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
							.toCharArray();
					// numbersAndLetters =
					// ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
				}
			}
		}
		char[] randBuffer = new char[length];
		for (int i = 0; i < randBuffer.length; i++) {
			randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
			// randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
		}
		return new String(randBuffer);
	}

	public static void main(String[] args) {
		try {
		 String [] encrypt =
		 ShortUrl.getUrl("http://url.cn/0cbJhs?type=1&from=19&u=liuzhibo2010&s=&f=1");
			 String str = Arrays.toString(encrypt);
			 System.out.println(str);

			 for(int i = 0; i < 4; i++){
					String key = encrypt[i];
					
				
					System.out.println(key) ;
					
				}			
//			for (int i = 0; i < 100; i++) {
//				new HttpThread(i * 100000).start();
//			}
//			Thread.sleep(120000);
			/*System.out.println("记录总数:" + ShortUrl.sumCount);
			System.out.println("分段密码总数:" + ShortUrl.pwdCount);
			System.out.println("分段密码重复条数:" + ShortUrl.sameCount);
			System.out.println("分段密码重复4次及以上条数:" + ShortUrl.sameCount4);*/
			// System.out.println(randomString(10));

			// System.out.println(Long.parseLong("f1f04b78",16));
			// long m = 0xf;
			// m = m << 4 | 0xf & 0xf1;
			// m = m << 4 | 0xf & 0xfff;
			// m = m << 4 | 0xf & 0xfff0;
			// m = m << 4 | 0xf & 0xffff4;
			// m = m << 4 | 0xf & 0xfffffb;
			// m = m << 4 | 0xf & 0xffffff7;
			// m = m << 4 | 0xf & 0xfffffff8;
			// System.out.println(m + "");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class HttpThread extends Thread {
	byte[] lock = new byte[0];
	byte[] lock1 = new byte[0];
	byte[] lock2 = new byte[0];
	int count = 0;
	int m = 0;

	public HttpThread(Integer count) {
		this.count = count;
	}

//	public void run() {
//		long start = System.currentTimeMillis();
//		// System.out.println("线程id：" + Thread.currentThread().getId() +
//		// "  开始时间：" + start);
//		for (int i = count; i < count + 100000; i++) {
//			try {
//				String tmp = ShortUrl.randomString(5);
//				if (ShortUrl.tables.get(tmp) != null) {
//					synchronized (lock) {
//						ShortUrl.sameCount++;
//					}
//				} else {
//					ShortUrl.tables.put(tmp, 1);
//				}
//			} catch (Exception e) {
//				System.out.println("###" + Thread.currentThread().getId());
//			}
//		}
//		long end = System.currentTimeMillis();
//		// System.out.println("线程id：" + Thread.currentThread().getId() +
//		// "  结束时间：" + end);
//		System.out.println("线程id：" + Thread.currentThread().getId() + "  运行时间："
//				+ (end - start) + "ms");
//	}
	public void run() {
		long start = System.currentTimeMillis();
		// System.out.println("线程id：" + Thread.currentThread().getId() +
		// "  开始时间：" + start);
		for (int i = count; i < count + 1; i++) {
			try {
				synchronized (lock2) {
					ShortUrl.sumCount++;
				}
				String[] tmp = ShortUrl.getUrl(ShortUrl.randomString(100));
				int n = 0;
				for (int j = 0; j < 4; j++) {
					synchronized (lock2) {
						ShortUrl.pwdCount++;
					}
					if (ShortUrl.tables.get(tmp[j]) != null) {
						synchronized (lock) {
							ShortUrl.sameCount++;
						}
						n++;
						// System.out.println("#" + ShortUrl.tables.get(tmp[j])
						// + "#");
						if (n > 3) {
							synchronized (lock1) {
								ShortUrl.sameCount4++;
							}
//							System.out.println("编码：" + tmp[j] + "重复次数" + n);
						}
						m++;
						continue;
					} else {
						ShortUrl.tables.put(tmp[j], 1);
						break;
					}
				}
			} catch (Exception e) {
				System.out.println("###" + Thread.currentThread().getId());
			}
		}
		long end = System.currentTimeMillis();
		// System.out.println("线程id：" + Thread.currentThread().getId() +
		// "  结束时间：" + end);
		System.out.println("线程id：" + Thread.currentThread().getId() + "  运行时间："
				+ (end - start) + "ms");
	}
}